import { Logger } from '@/utils/logger';
import { TaskBase } from '../TaskBase';

export class TaskReserve extends TaskBase {
    shouldRun(creep: Creep) {
        if (!('structureType' in this.target)) {
            return false;
        }
        const target = <StructureController>this.target;
        if (target.structureType !== STRUCTURE_CONTROLLER) {
            return false;
        }
        return !target.owner;   // 没有 owner 就行
    }
    _run(creep: Creep) {
        const result = creep.reserveController(<StructureController>this.target);
        if (result === OK) {
            return ERR_UNFINISHED;  // 无脑预定
        }
        else if (result === ERR_NOT_IN_RANGE) {
            return ERR_NOT_IN_RANGE;
        }
        else if (result === ERR_INVALID_TARGET) {
            // 已经被人撸了
            Logger.warn(`${creep.mark} 不能够预定房间 ${this.target.pos.roomName} 的控制器，因为控制器已经被占领`, 'task');
            return OK;
        }
        else {
            return OK;
        }
    }
}